package ThreadPoolCheck.ProducerConsumer;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by wangyg6 on 2017/11/9.
 */

class Test {
	public static void main(String[] args) {
		final DualSynch ds = new DualSynch();
		new Thread() {
			public void run() {
				//				int i = 0;
				while (true) {
					try {
						Thread.sleep((long) (Math.random() * 100));
//						System.out.println(Thread.currentThread().getName());
						ds.f(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
		new Thread() {
			public void run() {

				while (true) {
					try {
						Thread.sleep((long) (Math.random() * 100));
//						System.out.println(Thread.currentThread().getName());
						ds.f(2);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}.start();
	}

	static class DualSynch {
		Lock lock=new ReentrantLock();

		public void f(int flag) throws InterruptedException {
			synchronized (this) {
//			lock.lock();
				System.out.printf(Thread.currentThread().getName() + "f(%d)\n", flag);
				System.out.println(Thread.currentThread().getName() + "没人获取");
				Thread.sleep((long) Math.random() * 100);
				System.out.println(Thread.currentThread().getName() + "老子还是持有");
				System.out.println();
				System.out.println();
//			lock.unlock();
			}
		}
	}
}

